package cn.itcast.bos.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.base.CourierRepository;
import cn.itcast.bos.dao.base.FixeAreaRepository;
import cn.itcast.bos.dao.base.TakeTimeRepository;
import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.domain.base.TakeTime;
import cn.itcast.bos.service.FixeAreaService;

@Service
@Transactional
public class fixeAreaServiceImpl implements FixeAreaService{

	
	@Autowired
	private FixeAreaRepository fixeAreaRepository;
	
	@Autowired
	private TakeTimeRepository takeTimeRepository;
	
	@Autowired
	private CourierRepository courierRepository;
	
	
	@Override
	public void save(FixedArea model) {
		fixeAreaRepository.save(model);
	}

	@Override
	public Page<FixedArea> findAll(Specification<FixedArea> specification,
			Pageable pageable) {
		
		return fixeAreaRepository.findAll(specification, pageable);
	}

	
	// 添加快递员到定区
	@Override
	public void associationCourierToFixedArea(FixedArea model,
			Integer courierId, Integer takeTimeId) {
		
		// 注入快递员和TakeTimeRepository的dao成对象
		// 得到用户对象
		Courier courier = courierRepository.findOne(courierId);
		// 得到派送时间对象
		TakeTime takeTime = takeTimeRepository.findOne(takeTimeId);
		
		// 得到fixeArea对象
		FixedArea fixedArea = fixeAreaRepository.findOne(model.getId());
		// 将快递员绑定到定区
		fixedArea.getCouriers().add(courier);
		// 将派送时间绑定到快递员
		courier.setTakeTime(takeTime);
		
	}

}
